<?php
/**
 * Created by PhpStorm.
 * User: fengyan
 * Date: 17-7-5
 * Time: 下午5:55
 */

namespace App\Repositories\Admin;
use App\Models\Admin\Menu;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cache;
use App\Models;
use Illuminate\Support\Facades\DB;

class MenuRepository extends AdminRepository
{

    public function __construct()
    {
        parent::__construct();
        $this->model = new Models\Admin\Menu();
    }

    private static $instance;

    /**
     * 实现单例
     */
    public static function getInstance()
    {
        if (!(self::$instance instanceof self)) {
            self::$instance = new self();
        }

        return self::$instance;
    }

    /**
     * 获取实例查询模型
     * @param array $filter
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function getSelectModel(array $filter = []) : \Illuminate\Database\Eloquent\Builder
    {
        $model = $this->model;
        if (isset($filter['id']) && $filter['id'] !== null) {
            $model = $model->where('id', '=', $filter['id']);
        }

        if (isset($filter['pid']) && $filter['pid'] !== null) {
            $model = $model->where('pid', '=', $filter['pid']);
        }

        if (isset($filter['language']) && !empty($filter['language'])) {
            $model = $model->where('language', '=', $filter['language']);
        }

        if (isset($filter['menu_type']) && $filter['menu_type'] !== null) {
            $model = $model->where('menu_type', '=', $filter['menu_type']);
        }

        if (isset($filter['deleted_at']) && !empty($filter['deleted_at'])) {
            $model = $model->where('deleted_at', '=', $filter['deleted_at']);
        }

        if (isset($filter['is_developer'])) {
            $model = $model->where('is_developer', '=', $filter['is_developer']);
        }

        $model = $model->where('deleted_at', null);

        return $model;
    }

    /**
     * 根据pid获取菜单列表
     * @param null $pid
     * @param int $menuType
     * @param null $isDeveloper
     * @return \Illuminate\Database\Eloquent\Collection|static[]
     */
    public function getMenusList($pid = null, $menuType = 1, $isDeveloper = null)
    {
        return $this->getSelectModel([
            'pid' => $pid,
            'menu_type' => $menuType,
            'language' => config('app.locale', 'zh'),
            'is_developer' => $isDeveloper
        ])->orderBy('sort', 'asc')->orderBy('id','asc')->get();
    }

    /**
     * 获取全部导航列表
     * @return array
     */
    public function getMenusAllList()
    {
        if (Cache::has('menuList')) {
//            return Cache::get('menuList');
        }

        $uid = get_admin_current_user('id');

        if ($uid == config('admin.systems.administrator_uid')) {
            $list = $this->getMenusList()->toArray();
        } else {
            $slugList = (new PermissionRepository())->getNameListBuUserId($uid);

            $list = $this->getMenusList()->whereIn('slug', $slugList)->toArray();
        }

        $list = list_to_tree($list);

        Cache::forever('menuList', $list);

        return $list;
    }

    /**
     * 各个服务需实现自己的分页方法
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
     */
    public function getPaginateData($pid= null ): \Illuminate\Contracts\Pagination\LengthAwarePaginator
    {
        return $this->getSelectModel([
            'pid' => $pid,
            'language' => config('app.locale', 'zh'),
            'deleted_at' => null
        ])->orderBy('sort', 'asc')->paginate($this->pageSize);
    }

    /**
     * 根据逐渐编号获取详情
     * @param $id
     * @return mixed
     */
    public function getDetailById($id)
    {
        return $this->getSelectModel([
            'id' => $id,
            'language' => config('app.locale', 'zh'),
            'deleted_at' => null
        ])->first();
    }

    /**
     * 根据权限节点获取菜单列表
     * @param $permissionNameList
     * @return array
     */
    public function getMenusListBySlugList($permissionNameList)
    {
        return $this->getSelectModel()->whereIn('slug', $permissionNameList)->select(['id', 'name as display_name', 'slug as name'])->get()->toArray();
    }
}